package com.wframe.project.former.web.coupon;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import com.frame.database.JdataBean;
import com.frame.pageform.DataPageForm;
import com.wframe.project.former.dataservices.coupon.CouponService;

/**
 * 优惠劵管理
 *
 * @author lenovo
 */
public class CouponPage extends DataPageForm {

    private CouponService couponService = CouponService.getIntance();
    private JdataBean bean;

    @Override
    public String execute() throws Exception {
        setPagination(couponService.queryCoupon(getPageSize_(), getPageNum()));
        return "/webadmin/coupon/couponList.jsp";
    }

    @Override
    public String show() throws Exception {
        // TODO Auto-generated method stub
        if (bean != null && StringUtils.isNotBlank(bean.getString("id")))
            bean = couponService.getBean(bean.getString("id"));
        else {
            bean = new JdataBean();
            bean.put("state", 1);
        }
        return "/webadmin/coupon/couponEdit.jsp";
    }

    @Override
    public String toDel() throws Exception {
        // TODO Auto-generated method stub
        if (getPrimaryKey_() != null) {
            for (String id : getPrimaryKey_()) {
                Map key = new HashMap();
                key.put("id", id);
                JdataBean data = new JdataBean();
                data.put("state", 2);
                couponService.update(data.getDataMap(), key);
            }
        }
        return getJsonObject().toString();
    }

    @Override
    public boolean toUpdate() throws Exception {
        // TODO Auto-generated method stub
        if (StringUtils.isNotBlank(bean.getString("id"))) {
            Map key = new HashMap();
            key.put("id", bean.getString("id"));
            couponService.update(bean.getDataMap(), key);
        } else {
            bean.put("tag", 0);
            bean.put("type", 1);
            String source = bean.getString("source");
            if ("0".equalsIgnoreCase(StringUtils.trimToEmpty(source))) {
                source = null;
            }
            bean.put("source", source);
            couponService.save(bean.getDataMap());
        }
        getJsonObject().getJsonPage().closeWin("ok");
        return false;
    }

    /**
     * 下架
     *
     * @return
     * @throws Exception
     */
    public String offshelf() throws Exception {
        if (getPrimaryKey_() != null) {
            for (String id : getPrimaryKey_()) {
                Map key = new HashMap();
                key.put("id", id);
                JdataBean data = new JdataBean();
                data.put("state", 0);
                couponService.update(data.getDataMap(), key);
            }
        }
        return getJsonObject().toString();
    }

    /**
     * 发送优惠劵
     *
     * @return
     * @throws Exception
     */
    public String send() throws Exception {
        couponService.send(bean.getString("id"), bean.getString("userids").split(","));
        getJsonObject().put("state", "ok");
        return getJsonObject().toString();
    }

    public JdataBean getBean() {
        return bean;
    }

    public void setBean(JdataBean bean) {
        this.bean = bean;
    }

}
